export default {
  title: '员工列表',
  type: 'table',
  permission: '/system/employee/list',
  config: {
    dataUrl: '/system/employee/list',

    filter: {
      controls: [
        {
          key: 'createdDate',
          label: '创建时间',
          type: 'dateRange'
        },
        {
          key: 'nameContains',
          label: '姓名',
          type: 'text'
        },
        {
          key: 'phoneNumberContains',
          label: '手机号',
          type: 'text'
        },
        {
          key: 'genderIs',
          label: '性别',
          type: 'select',
          config: {
            options: ['未知', '男', '女', '保密'].map(text => ({
              text,
              value: text
            }))
          }
        },
        {
          key: 'lockedIs',
          label: '是否禁用',
          type: 'select',
          config: {
            options: [
              {
                text: '已禁用',
                value: true
              },
              {
                text: '正常',
                value: false
              }
            ]
          }
        }
      ]
    },

    actions: [
      {
        key: 'create',
        text: '添加员工',
        icon: 'plus',
        type: 'form',
        permission: '/system/employee/create',
        config: {
          color: 'primary',
          title: '添加员工',
          submitUrl: '/system/employee/create',
          submitSuccessMessage: "添加成功",
          submitText: '确认添加',
          controls: [
            {
              key: 'phoneNumber',
              label: '手机号',
              type: 'text',
              required: true,
              config: {
                rules: ['phoneNumber']
              }
            },
            {
              key: 'name',
              label: '姓名',
              type: 'text',
              required: true,
              config: {
                rules: [
                  {max: 30, message: '最多输入 30 个字符'}
                ]
              }
            },
            {
              key: 'gender',
              label: '性别',
              type: 'radio',
              config: {
                initialValue: '未知',
                options: ['未知', '男', '女', '保密'].map(text => ({
                  text,
                  value: text
                }))
              }
            },
            {
              key: 'locked',
              label: '是否禁用',
              type: 'switch',
              required: true,
              config: {
                initialValue: false
              }
            }
          ]
        }
      }
    ],

    columns: [
      {
        key: 'createdDate',
        title: '创建时间',
        type: 'datetime',
        sorter: true,
        defaultSortOrder: 'descend'
      },
      {
        key: 'name',
        title: '姓名'
      },
      {
        key: 'phoneNumber',
        title: '手机号'
      },
      {
        key: 'roles',
        title: '角色',
        type: 'component',
        config: {
          component: {
            props: ['row'],
            render() {
              return <ul className="m-0 pl-6">
                {this.row.roles.map(role => <li>
                  {role.name}
                </li>)}
              </ul>
            },
          }
        }
      },
      {
        key: 'gender',
        title: '性别'
      },
      {
        key: 'locked',
        title: '是否禁用',
        type: 'enum',
        config: {
          constants: {
            true: {
              text: '已禁用',
              color: 'orange'
            },
            false: {
              text: '正常',
              color: 'green'
            }
          }
        }
      },
    ],

    rowActions: [
      {
        key: 'edit',
        text: '修改',
        type: 'form',
        permission: '/system/employee/edit',
        config: {
          color: 'primary',
          title: '修改员工',
          initUrl: '/system/employee/edit/get',
          submitUrl: '/system/employee/edit',
          submitText: '确认修改',
          submitSuccessMessage: "修改成功",
          controls: [
            {
              key: 'phoneNumber',
              label: '手机号',
              type: 'text',
              required: true,
              config: {
                rules: 'phoneNumber'
              }
            },
            {
              key: 'name',
              label: '姓名',
              type: 'text',
              required: true
            },
            {
              key: 'gender',
              label: '性别',
              type: 'radio',
              config: {
                options: ['未知', '男', '女', '保密'].map(text => ({
                  text,
                  value: text
                }))
              }
            },
            {
              key: 'locked',
              label: '是否禁用',
              type: 'switch',
              required: true,
              config: {
                initialValue: false
              }
            }
          ]
        }
      },
      {
        key: 'grantRoles',
        text: '分配角色',
        type: 'form',
        permission: '/system/employee/grant',
        config: {
          color: 'primary',
          title: '分配角色',
          initUrl: "/system/employee/grant/get",
          submitUrl: "/system/employee/grant",
          submitText: '确认分配',
          submitSuccessMessage: '分配成功',
          controls: [
            {
              key: 'roleIds',
              label: '角色',
              type: 'checkbox',
              config: {
                options: 'roles',
                textKey: 'name',
                valueKey: 'id'
              }
            }
          ]
        }
      },
      {
        key: 'lock',
        text: '禁用',
        type: 'confirm',
        permission: '/system/employee/lock',
        visible({selectedRows}) {
          const {locked, admin} = selectedRows[0];
          return !locked && !admin;
        },
        config: {
          color: 'danger',
          submitColor: 'danger',
          submitUrl: '/system/employee/lock',
          submitSuccessMessage: '禁用成功'
        }
      },
      {
        key: 'unlock',
        text: '启用',
        type: 'confirm',
        permission: '/system/employee/unlock',
        visible({selectedRows}) {
          return selectedRows[0].locked;
        },
        config: {
          color: 'danger',
          submitColor: 'danger',
          submitUrl: '/system/employee/unlock',
          submitSuccessMessage: '启用成功'
        }
      },
      {
        key: 'reset-password',
        text: '重置密码',
        type: 'confirm',
        permission: '/system/employee/reset-password',
        visible({selectedRows}) {
          const {admin} = selectedRows[0];
          return !admin;
        },
        config: {
          submitUrl: '/system/employee/reset-password',
          submitSuccessMessage: ({password}) => `登录密码已重置为：${password}`
        }
      },
    ]
  }
};
